import {encrypt,decrypt} from '@/fetch/crypto.js';
import {key,doublekey} from '@/fetch/config.js';
//判断一个元素在不在数组内
export let arrContain = function (arr, element) {
  let flag = false;
  if (arr.length > 1) {
    for (var i = 0; i < arr.length; i++) {
      if (arr[i] == element) {
        flag = true
      }
    }
  }
  return flag;
}

// 两数组重复的元素
export let getRepeat = function (arr1, arr2) {
  let newArr = [];
  if (arr1.length > 0 && arr2.length > 0) {
    for (let i = 0; i < arr2.length; i++) {
      for (let j = 0; j < arr1.length; j++) {
        if (arr1[j] === arr2[i]) {
          newArr.push(arr1[j]);
        }
      }
    }
  }
  return newArr;
}

/**
 * cookie 获取
 */
export function getCookie(name) {
  var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
  arr = document.cookie.match(reg);
  if (arr) {
    var cookieValue = decrypt(arr[2], key, doublekey);
    // 值有多个数据 
    if (unescape(cookieValue).indexOf('&') != -1) {
      var obj = {};
      var list = unescape(cookieValue).split('&');
      list.forEach((ele, index) => {
        var item = ele.split('=');
        obj[item[0]] = item[1];
      });
      return obj;
    } else {
      //值有一个数据
      return unescape(cookieValue);
    }
  } else {
    return null;
  }
}
/**
 * cookie 设置
 *     cname 存入的参数名
 *     value  存入的值
 *     exdays  过期时间
 */
export function setCookie(cname, cvalue, exdays) {
  var d = new Date();
  d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
  var expires = "expires=" + d.toGMTString();
  var isObject = Object.prototype.toString.call(cvalue) === '[object Object]';
  if (isObject) {
    let value = '';
    for (var i in cvalue) value = (value ? value + '&' : value) + i + '=' + escape(cvalue[i]);
    document.cookie = cname + "=" + encrypt(value, key, doublekey) + "; " + expires + ";path=/";
  } else {
    document.cookie = cname + "=" + escape(cvalue) + "; " + expires + ";path=/";
  }
};

//删除cookie
export function delCookie(name) {
  setCookie(name, "", -1);
};

/**
 * cookie 解析用户cookie
 */
export function getUser(value) {
  if (!value) return null;
  value = unescape(decrypt(value, key, doublekey));
  if (!value || value == 'undefined' || value == 'null') {
    return null;
  } else {
    var obj = {};
    var list = value.split('&');
    list.forEach((ele, index) => {
      var item = ele.split('=');
      obj[item[0]] = item[1];
    });
    return obj;
  }
}

/**
 * 左右滚动 
 * ['roll-box', 'roll-main', 'roll-ul1', 'roll-ul2'] , type: 'clear'
 */
export function scrollLeft(domData,type) {
  if (process.client) {
    var flag = null;
    //清除定时器
    if (type === 'clear') {
      clearInterval(flag);
      flag = null;
      return;
    }
    //定时器
    var speed = 50;
    var obox = document.getElementById(domData[0]);
    var omain = document.getElementById(domData[1]);
    var oleft = document.getElementById(domData[2]);
    var oright = document.getElementById(domData[3]);
    oright.innerHTML = oleft.innerHTML;
    
    function theScroll() {
      if (obox.scrollLeft >= oleft.offsetWidth) {
        obox.scrollLeft = 0;
      } else {
        obox.scrollLeft++;
      }
    }
    flag = setInterval(theScroll, speed);

    omain.onmouseover = function () {
      clearInterval(flag);
    }
    omain.onmouseout = function () {
      flag = setInterval(theScroll, speed);
    }
  }
}

// 获取本月数据
export function getNowDate(year,month) {
  //设置空数据
  function setUndefined(num) {
    let list = [];
    for (let i = 0; i < num; i++) {
      list[i] = '';
    }
    return list;
  };

  //某月 天数
  function mGetDate(year,month) {
    var d = new Date(year,month,0);
    return d.getDate();
  };
  //格式化日期
  function formatDate(val) {
    let week = ['星期日','星期一','星期二','星期三','星期四','星期五','星期六日'];
    let date = new Date(val);
    let obj = {
      'y': date.getFullYear(), //年
      "M": date.getMonth() + 1, //月份   
      "d": date.getDate(), //日
      'w': date.getDay(),//周几
    };
    obj['M'] < 10 ? obj['M'] = "0" + obj['M'] : obj['M'];
    obj['d'] < 10 ? obj['d'] = "0" + obj['d'] : obj['d'];
    
    let dateObj = {
      date: obj['y'] + '-' + obj['M'] + '-' + obj['d'],
      week: week[obj['w']],
      weekNum: obj['w'],
    }
    
    return dateObj;
  };

  let list = [];
  let totalDay = mGetDate(year,month);
  
  for(let i = 1; i <= totalDay ; i++) {
    let item = formatDate(year + '-' + month + '-' + (i < 10 ? '0' + i : i) + ' 00:00:00');
    
    list[i-1] = item;
  }
  
  if (list[0].weekNum > 0) {
    let list_first = setUndefined(list[0].weekNum);
    list = list_first.concat(list);
  }
  if (list[list.length-1].weekNum <= 6) {
    let list_last = setUndefined(42-list.length);
    list = list.concat(list_last);
  }
  return list;
}

// 对登录用户 权限的处理
export function getPermissions(userObj) {
  let czAuthorityObj = {
    isForecaster: false, //是否是预测师
    zcForecasterNum: 0,//彩种是预测师的个数 
    // 1 用户  、 2 免费预测师 、3 收费预测师 、4 推广员 、 5 枪手
    1: 1,//福彩3D
    2: 1,//排列三
    3: 1,//双色球
    4: 1,//大乐透
  };
  if (userObj && (userObj.Permissions == 2 || userObj.Permissions == 3)) {
    czAuthorityObj.isForecaster = true;
    if(userObj.Permissions_Sd == 2 || userObj.Permissions_Sd == 3) czAuthorityObj.zcForecasterNum = czAuthorityObj.zcForecasterNum + 1;//福彩3D
    if(userObj.Permissions_P3 == 2 || userObj.Permissions_P3 == 3) czAuthorityObj.zcForecasterNum = czAuthorityObj.zcForecasterNum + 1;//排列三
    if(userObj.Permissions_Ssq == 2 || userObj.Permissions_Ssq == 3) czAuthorityObj.zcForecasterNum = czAuthorityObj.zcForecasterNum + 1;//双色球
    if(userObj.Permissions_Dlt == 2 || userObj.Permissions_Dlt == 3) czAuthorityObj.zcForecasterNum = czAuthorityObj.zcForecasterNum + 1;//大乐透
    czAuthorityObj[1] = userObj.Permissions_Sd;
    czAuthorityObj[2] = userObj.Permissions_P3;
    czAuthorityObj[3] = userObj.Permissions_Ssq;
    czAuthorityObj[4] = userObj.Permissions_Dlt;
  }
  return czAuthorityObj;
}